﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using com.tiyma.common;
using System.IO;
using System.Net;
using com.tiyma.model;
using System.Web;

namespace com.tiyma.components
{
    public class BookChapterManager
    {  
       


        /// <summary>
        /// 搜索普通章节
        /// </summary>
        /// <param name="i_site"></param>
        /// <param name="i_chpater"></param>
        public string SearchNomalChapter(ChapterInfo i_chapter,int i_site_id)
        {
            if (i_site_id != Consts.qdmm && i_site_id != Consts.qidian)
            {
                BookManager t_book_mgr=new BookManager();
                SiteInfo t_site_info = t_book_mgr.GetSiteInfo(i_site_id);
                if (t_site_info == null)
                    return string.Empty;

                return SearchSiteChapter(t_site_info, i_chapter);
            }

            long t_book_id = 0;
            long t_chapter_id = 0;

            string t_chapter_url = string.Empty;

            string t_chapter_content = string.Empty;

            Match t_mc = Regex.Match(i_chapter.ChapterUrl, "(\\d+),(\\d+)\\.aspx", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            if (t_mc.Groups.Count > 1)
            {
                if (!long.TryParse(t_mc.Groups[1].Value.ToString(), out t_book_id))
                    t_book_id = 0;
                if (!long.TryParse(t_mc.Groups[2].Value.ToString(), out t_chapter_id))
                    t_chapter_id = 0;
            }

            if (t_chapter_id == 0)
                return string.Empty;

            try
            {
                t_chapter_url = string.Format("http://files.qidian.com/Author{0}/{1}/{2}.txt", (t_book_id % 8 + 1), t_book_id, t_chapter_id);

                t_chapter_content = HttpHelper.GetHttpRequestStringByNUll_Post(t_chapter_url);

                t_mc = Regex.Match(t_chapter_content, "document.write\\(\\'(.*?)\\'\\)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                if (t_mc != null && t_mc.Groups.Count > 0)
                {
                    t_chapter_content = t_mc.Groups[1].Value.ToString();
                }

                t_chapter_url = i_chapter.ChapterUrl;

                if (!string.IsNullOrEmpty(t_chapter_content))
                {
                    t_chapter_content = Regex.Replace(t_chapter_content, "<a[^<>]*?>.*?</a>", "");
                    bool t_save_ret = NetSiteCatchManager.WriteBookContent(AppConfig.ChapterRoot,Convert.ToInt64( i_chapter.BookId), Convert.ToInt64( i_chapter.ChapterId), t_chapter_content);

                    if (t_save_ret)
                    {
                        BookManager t_book_mgr = new BookManager();
                        BookUrlInfo t_book_url = new BookUrlInfo();
                        t_book_url.ChapterListUrl = "";
                        t_book_mgr.UpdateChapter(Convert.ToInt64(i_chapter.BookId), Convert.ToInt64(i_chapter.ChapterId),t_chapter_url, t_book_url);
                        
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("搜索普通章节失败" + ex.ToString());
            }

            return t_chapter_content;
        }


        /// <summary>
        /// 获取其他站点的普通章节
        /// </summary>
        /// <param name="i_site_info"></param>
        /// <param name="i_chapter"></param>
        /// <returns></returns>
        private string SearchSiteChapter(SiteInfo i_site_info, ChapterInfo i_chapter)
        {
            string t_chapter_content = string.Empty;
            try
            {
                t_chapter_content = HttpHelper.GetHttpRequestStringByNUll_Post(i_chapter.ChapterUrl);

                if (!string.IsNullOrEmpty(t_chapter_content))
                {
                    Match t_mc = Regex.Match(t_chapter_content, i_site_info.ChapterContentReg, RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (t_mc != null && t_mc.Groups.Count >= 0)
                    {
                        t_chapter_content = t_mc.Groups[1].Value.ToString();

                        if (!string.IsNullOrEmpty(i_site_info.ChapterContentFilter))
                        {
                            string[] t_filter_arr = i_site_info.ChapterContentFilter.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);
                            foreach (string t_filter_reg in t_filter_arr)
                            {
                                t_chapter_content = Regex.Replace(t_chapter_content, t_filter_reg, "", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                            }
                        }
                    }
                }

                if (!string.IsNullOrEmpty(t_chapter_content))
                {
                    t_chapter_content = Regex.Replace(t_chapter_content, "<a[^<>]*?>.*?</a>", "");
                    bool t_save_ret = NetSiteCatchManager.WriteBookContent(AppConfig.ChapterRoot, Convert.ToInt64(i_chapter.BookId), Convert.ToInt64(i_chapter.ChapterId), t_chapter_content);

                    if (t_save_ret)
                    {
                        BookManager t_book_mgr = new BookManager();
                        BookUrlInfo t_book_url = new BookUrlInfo();
                        t_book_url.ChapterListUrl = "";
                        t_book_mgr.UpdateChapter(Convert.ToInt64(i_chapter.BookId), Convert.ToInt64(i_chapter.ChapterId), i_chapter.ChapterUrl, t_book_url);
                        

                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("获取站点普通章节失败" + ex.ToString());
            }

            return t_chapter_content;
        }

      
    }
}
